12C - Fruits - CodeForces Solution


greedy implementation sortings *1100

Please click on ads to support us..

Python Code:

import sys; R = sys.stdin.readline
n,m = map(int,R().split())
a = sorted(map(int,R().split()))
dic = {}
for _ in range(m):
    s = R()
    if s not in dic: dic[s] = 1
    else: dic[s] += 1
dic = sorted(dic.values(),reverse=True)
res1,res2 = 0,0
for i in range(len(dic)):
    res1 += a[i]*dic[i]
    res2 += a[-i-1]*dic[i]
print(res1,res2)

C++ Code:

#include<bits/stdc++.h>
using namespace std;
int a[200];
map<string,int>mp;
struct code{
	string s;
	int q;
}b[200];
int n,m;
int mx;
int mn;
int qwe[200];
int c[200];
bool cmp(code x,code y)
{
	return x.q>y.q;
}
void f(int x)
{
	if(x==m+1)
	{
		int ans=0;
		for(int i=1;i<=m;i++)
		{
			ans+=c[i]*b[i].q;
		}
		mx=max(mx,ans);
		mn=min(mn,ans);
		return;
	}
	for(int i=1;i<=n;i++)
	{
		if(qwe[i]==0)
		{
			qwe[i]=1;
			c[x]=a[i];
			f(x+1);
			qwe[i]=0;
		}
	}
	return;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	sort(a+1,a+n+1);
	for(int i=1;i<=m;i++)
	{
		cin>>b[i].s;
		if(mp[b[i].s])
		{
			b[mp[b[i].s]].q+=1;
			m--;
			i--;
		}
		else
		{
			mp[b[i].s]=i;
			b[i].q+=1;
		}
	}
	sort(b+1,b+m+1,cmp);
//	printf("\n");
//	for(int i=1;i<=m;i++)
//	{
//		cout<<b[i].s<<" "<<b[i].q<<endl;
//	}
	for(int i=1;i<=m;i++)
	{
		mx+=b[i].q*a[n-i+1];
	}
	for(int i=1;i<=m;i++)
	{
		mn+=b[i].q*a[i];
	}
	printf("%d %d",mn,mx);
	return 0;
}


Comments

Submit
0 Comments
More Questions

1009A - Game Shopping
1657A - Integer Moves
230B - T-primes
630A - Again Twenty Five
1234D - Distinct Characters Queries
1183A - Nearest Interesting Number
1009E - Intercity Travelling
1637B - MEX and Array
224A - Parallelepiped
964A - Splits
1615A - Closing The Gap
4C - Registration System
1321A - Contest for Robots
1451A - Subtract or Divide
1B - Spreadsheet
1177A - Digits Sequence (Easy Edition)
1579A - Casimir's String Solitaire
287B - Pipeline
510A - Fox And Snake
1520B - Ordinary Numbers
1624A - Plus One on the Subset
350A - TL
1487A - Arena
1520D - Same Differences
376A - Lever
1305A - Kuroni and the Gifts
1609A - Divide and Multiply
149B - Martian Clock
205A - Little Elephant and Rozdil
1609B - William the Vigilant